package src.cn.hquanheng.week3;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class TestPalettle extends Application{

	private double red;
	private double green;
	private double yellow;
	
	private Stage stage;//�´��� ��ɫ��
	private Rectangle rec;

	@Override
	public void start(Stage primaryStage) throws Exception {
		Scene scene = new Scene(creatUI(),420,360);
		
		primaryStage.setScene(scene);
		primaryStage.setTitle("ɫ��");
		primaryStage.show();
	}

	private Parent creatUI() {
		VBox vbox = new VBox();
		vbox.setPadding(new Insets(10));
		vbox.setSpacing(10);
		rec = new Rectangle(400,300);
		Button button = new Button("��ɫ��");
		button.setOnAction((e)->{
			stage = new Stage();
			Scene scene = new Scene(creatNewUi(),220,320);
			stage.setScene(scene);
			stage.setTitle("��ɫ��");
			stage.show();
		});
		vbox.getChildren().addAll(rec,button);
		return vbox;
	}
private Parent creatNewUi() {
		VBox vbox = new VBox();
		
		Slider s1= new Slider(0,255,128);
		Slider s2= new Slider(0,255,128);
		Slider s3= new Slider(0,255,128);
		
		red = s1.getValue();
		green = s2.getValue();
		yellow = s3.getValue();
		
		Label lRed = new Label(Double.toString(red));
		Label lGreen = new Label(Double.toString(green));
		Label lYellow = new Label(Double.toString(yellow));
			
		HBox hbox = new HBox();
		hbox.getChildren().addAll(lRed,lGreen,lYellow);
		hbox.setSpacing(10);
		hbox.setPadding(new Insets(10));
		
		Circle circle = new Circle();
		circle.setRadius(50);
		circle.setFill(Color.rgb((int)red,(int) green,(int) yellow));
		
		s1.valueProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				lRed.setText(String.format("%.2f", newValue));
				red=newValue.doubleValue();
				System.out.println(red+" "+green+ " "+yellow);
				circle.setFill(Color.rgb((int)red,(int) green,(int) yellow));
			}
		});
		
		s2.valueProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				lGreen.setText(String.format("%.2f", newValue));
				green=newValue.doubleValue();
				System.out.println(red+" "+green+ " "+yellow);
				circle.setFill(Color.rgb((int)red,(int) green,(int) yellow));
			}
		});
		
		s3.valueProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				lYellow.setText(String.format("%.2f", newValue));
				yellow=newValue.doubleValue();
				System.out.println(red+" "+green+ " "+yellow);
				circle.setFill(Color.rgb((int)red,(int) green,(int) yellow));
			}
		});
		
		vbox.getChildren().add(hbox);
		
		
		vbox.getChildren().addAll(circle,s1,s2,s3);
		vbox.setSpacing(10);
		vbox.setPadding(new Insets(10));
		
		Button button = new Button("����");
		button.setOnAction((e)->{
			rec.setFill(Color.rgb((int)red, (int)green, (int)yellow));
			stage.hide();		
		});
		vbox.getChildren().add(button);
		return vbox;
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}
